<?php

class AdministracaoDao extends Dao {

    /**
     * @var DependenteService
     */
    private $dependenteService;
    /**
     * @var UsuarioService
     */
    private $usuarioService;

    public function injectDependencies() {
        $this->dependenteService = DependenteService::getInstance();
        $this->usuarioService = UsuarioService::getInstance();
    }

    public function findUserById($IDUsuario) {
        return $this->usuarioService->findById($IDUsuario);
    }

    /**
     * @method  updateEmployee
     *
     * Realiza a alteracao completa do usuario
     *
     * @param   array    $ArDados   Todos os dados do usuario separados por Dependente, Administracao, Funcao, Usuario
     *
     * @author  Richard Brandao <richardsbrandao@gmail.com>
     * @return  boolean
     * @since   2012-12-12
     */
    public function updateEmployee($ArDados) {
       $LoggedUser = Dracosys::getLoggedUser();
       $Usuario = $this->findUserById($ArDados['IDUsuario']);
       $Usuario->tMysqli->getConnection(MYSQL_HOST, MYSQL_USER,MYSQL_PASS, MYSQL_DB);
       $Usuario->tMysqli->beginTransaction();
       $Usuario->tMysqli->escape($ArDados);

       $StSql = TQuery::getSqlUpdateUserData($ArDados, $ArDados['IDUsuario']);
       $Usuario->tMysqli->exec($StSql);

       $ArDependentData = $this->dependenteService->getDependentUpdateList($ArDados['Dependente'], $Usuario->getDependentes());
       $Usuario->updateDependents($ArDependentData, $this->dependenteService);

       $Usuario->tMysqli->endTransaction();
       $BoReturn = $Usuario->tMysqli->lastTransactionReturn();
       $Usuario->tMysqli->close();

       if( $Usuario->equals($LoggedUser) ) {
           Dracosys::saveSessionValue(SESSION_SYSTEM, 'Usuario', $Usuario);
       }

       return $BoReturn;
    }
    //15-01-12
    public function saveEmployee($ArDados) {
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);

        $StSqlInsertUserData = TQuery::getSqlInsertUserData($ArDados['Pessoal'], $ArDados['Funcao']['IDPerfil']);
        $this->tMysqli->exec($StSqlInsertUserData);
        $IDUsuario = $this->tMysqli->lastInsertId();

        $StSqlInsertAdmData = TQuery::getSqlInsertAdmData($ArDados['Administrativo'], $IDUsuario);
        $this->tMysqli->exec($StSqlInsertAdmData);
        $IDAdministracao = $this->tMysqli->lastInsertId();

        if(!empty($ArDados['Dependente'])) {
            foreach($ArDados['Dependente'] as $ArDependente) {
                $Dependent = new Dependente(false, $IDAdministracao, $ArDependente['StNomeDependente'], $ArDependente['DtNascimentoDependente'], $ArDependente['StGrauParentesco'], $this->tMysqli);
                $this->tMysqli->exec(TQuery::getSqlInsertDependent($Dependent));
            }
        }

        $StSqlInsertUserSector = TQuery::getSqlInsertUserSector($ArDados['Funcao']['IDSetor'], $IDUsuario);
        $this->tMysqli->exec($StSqlInsertUserSector);

        $StSqlInsertPreference = TQuery::getSqlInsertPreferences($ArDados['Preferencias'], $IDUsuario);
        $this->tMysqli->exec($StSqlInsertPreference);

        if( !empty($ArDados['IDFerramentas']) ) {
            $ArFerramentasHabilitadas = explode(', ', $ArDados['IDFerramentas']);
            if(!empty($ArFerramentasHabilitadas)) {
                foreach($ArFerramentasHabilitadas as $IDFerramenta) {
                    $this->tMysqli->exec(TQuery::getSqlEnableTool($IDUsuario, $IDFerramenta));
                }
            }
        }

        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function getNumberOfEmployees() {
        $this->initConnection();
        $StSql = TQuery::getSqlNumberOfEmployees();
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'NumberOfEmployees');
    }

    public function getTotalSalarialSheet() {
        $this->initConnection();
        $StSql = TQuery::getSqlTotalSalarialSheet();
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        return $this->tMysqli->result(0, 'TotalSalarialSheet');
    }

    public function getTotalSheetByPosition($Position) {
        $this->initConnection();
        $StSql = TQuery::getSqlTotalSalarialSheetByPosition($Position);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        $positionSalarialSheet = $this->tMysqli->result(0 , 'TotalSalarialSheet');
        return is_numeric($positionSalarialSheet) ? $positionSalarialSheet : 0 ;
    }

    public function getTotalSheetByDepartament($Departament) {
        $this->initConnection();
        $StSql = TQuery::getSqlTotalSalarialSheetByDepartament($Departament);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        $departamentSalarialSheet = $this->tMysqli->result(0 , 'TotalSalarialSheet');
        return is_numeric($departamentSalarialSheet) ? $departamentSalarialSheet : 0 ;
    }

    public function getTotalSheetBySector($Sector) {
        $this->initConnection();
        $StSql = TQuery::getSqlTotalSalarialSheetBySector($Sector);
        $this->tMysqli->exec($StSql);
        $this->closeConnection();
        $sectorSalarialSheet = $this->tMysqli->result(0 , 'TotalSalarialSheet');
        return is_numeric($sectorSalarialSheet) ? $sectorSalarialSheet : 0 ;
    }

    public function getLastFiredEmployees() {
        $this->initConnection();
        $StSql = TQuery::getSqlLastFiredEmployees();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArUsuarios = array();
        while($ArIdsUsuario = mysqli_fetch_assoc($tQuery)) {
            $ArUsuarios[] = new Usuario($ArIdsUsuario['IDUsuario'], $this->tMysqli);
        }
        $this->closeConnection();
        return $ArUsuarios;
    }

    public function getLastRegisteredEmployees() {
        $this->initConnection();
        $StSql = TQuery::getSqlLastRegisteredEmployees();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArUsuarios = array();
        while($ArIdsUsuario = mysqli_fetch_assoc($tQuery)) {
            $ArUsuarios[] = new Usuario($ArIdsUsuario['IDUsuario'], $this->tMysqli);
        }
        $this->closeConnection();
        return $ArUsuarios;
    }
}

?>
